﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;
using NetWork.Packets;

namespace UI
{
    //0表示未到日期，1表示可以领取，2表示普通领取，3代表双倍领取

    public class UIActivitySignIcon : MonoBehaviour
    {
        public GameObject icon;
        [HideInInspector]
        public UIEventTrigger evt;
        public UILabel vipNeed;
        public GameObject getMask;
        MonthRewardItem item;
        public GameObject effect;
        int vipLevel;

        public ParticleSystem ps;
        public bool EffectActived
        {
            set
            {
                this.effect.SetActive(value);
            }
        }

        UIActivityEveryDaySign sign;
        public void Init(MonthRewardItem item,int curr,UIActivityEveryDaySign sign)
        {
            this.item = item;

            var obj = Instantiate(icon);
            obj.SetActive(false);
            obj.transform.SetParent(transform);
            obj.transform.localScale = Vector3.one;
            obj.transform.localPosition = Vector3.zero;
            obj.transform.localRotation = Quaternion.Euler(Vector3.zero);
            obj.SetActive(true);
            evt = obj.AddComponent<UIEventTrigger>();

            var script = obj.GetComponent<CommonIcon>();

            string id = "";
            string num = "";

            this.sign = sign;
            //this.effect.SetActive(false);
            EffectActived = false;

#if UNITY_EDITOR
            Debug.Log("[DEBUG] 签到信息:" + item.rewardId +" 状态:"+item.getState+" day:"+item.day+" vip level  need:"+item.doubleNeedVipLevel);
#endif

            if (item.rewardId.Contains(","))
            {
                var ss = item.rewardId.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
                id = ss[0];
                num = ss[1];
            }
            else
            {
                id = item.rewardId;
                num = "1";
            }

            if (item.doubleNeedVipLevel > 0)
            {
                vipNeed.transform.parent.gameObject.SetActive(true);
                vipNeed.text = string.Format(TableTextBlo.GetText("sign_vip_need"),item.doubleNeedVipLevel.ToString());
            }
            else
            {
                vipNeed.transform.parent.gameObject.SetActive(false);
            }

            script.SetData(id, false, int.Parse(num));

            vipLevel = StaticData.playerData.vip;

            getMask.SetActive(false);

            if (item.day == curr)
            {
                if(item.getState == 1)
                {
                    evt.onClick = new System.Collections.Generic.List<EventDelegate>();
                    evt.onClick.Add(new EventDelegate(()=> 
                    {
                        NetAPI.SendHttp(OpDefine.CSReceiveMonthSign, new CSReceiveMonthSign(), cb );
                    }));
                    //this.effect.SetActive(true);
                    EffectActived = true;
                }
                else if(item.getState==2)
                {
                    if (vipLevel < item.doubleNeedVipLevel && item.doubleNeedVipLevel > 0)
                    {
                        evt.onClick.Clear();
                        getMask.SetActive(true);
                        evt.onClick.Add(new EventDelegate(OpenVip));                        
                    }
                    else if(vipLevel >= item.doubleNeedVipLevel && item.doubleNeedVipLevel > 0)
                    {
                        getMask.SetActive(false);
                        evt.onClick = new System.Collections.Generic.List<EventDelegate>();
                        evt.onClick.Add(new EventDelegate(() =>
                        {
                            NetAPI.SendHttp(OpDefine.CSReceiveMonthSign, new CSReceiveMonthSign(), cb);
                        }));
                        //this.effect.SetActive(true);
                        EffectActived = true;
                    }
                    else 
                    {
                        getMask.SetActive(true);
                        Destroy(evt);
                    }
                }
                else if(item.getState == 3)
                {
                    getMask.SetActive(true);
                    Destroy(evt);
                }
            }
            else
            {
                if (item.getState == 0)
                {
                    getMask.SetActive(false);
                    evt.onClick = new System.Collections.Generic.List<EventDelegate>();
                    evt.onClick.Add(new EventDelegate(() => 
                    {
                        UIItemType itemType = UIItemType.NONE;
                        string iconName = "";
                        int qulity = -1;
                        CommonMethod.IconParse(id, out itemType, out iconName, out qulity);
                        CommonMethod.ShowDescription(itemType, id);
                    }));
                    
                }
                else
                {
                    getMask.SetActive(true);
                    Destroy(evt);
                }
            }

        }

        void PopupBoxInfo()
        {
            string tip = TableTextBlo.GetText("vip_sign_recharge");

            tip = string.Format(tip, item.doubleNeedVipLevel);

            BoxManager.CreateMessageBoxWithCofirmAndCancelButton(tip, (b) =>
            {
                if (b)
                {
                    UI.PageManager.Instance.OpenPage("ChargePage", "");
                }
            });
        }

        void cb(Packet o, bool ok)
        {
            if (ok)
            {
                var recv = (SCReceiveMonthSign)o.kBody;
                StaticData.playerData.AddResultObj(recv.get);
                
                sign.signCount.text = sign.currentDay + "/" + sign.datas.Count;

                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("sign_success_get_reward_item"));

                //this.effect.SetActive(false);
                EffectActived = false;
                StaticData.redTips.SetPointState(RedPointTip.Red.dailySignOrdinary, 0);
                if (vipLevel < item.doubleNeedVipLevel && item.doubleNeedVipLevel > 0)
                {
                    evt.onClick.Clear();
                    getMask.SetActive(true);
                    evt.onClick.Add(new EventDelegate(PopupBoxInfo));
                }
                else
                {
                    getMask.SetActive(true);
                    Destroy(evt);
                }
            }

        }

        void OpenVip()
        {
            string tip = TableTextBlo.GetText("vip_sign_recharge");

            tip = string.Format(tip, item.doubleNeedVipLevel);

            BoxManager.CreateMessageBoxWithCofirmAndCancelButton(tip, (b) =>
            {
                if (b)
                {
                    UI.PageManager.Instance.OpenPage("ChargePage", "");
                }
            });
        }

        #region 处理Sign的Icon的效果穿透问题
        public UIScrollView scrollView;

        float timer = 0;
        const float haoLongCheck=0.3f;

        private int minHeight = -64-395;
        private int maxHeight = -64;
        void Update()
        {
            timer += Time.deltaTime;
            if(timer > haoLongCheck)
            {
                timer = 0f;
                CheckIconCompleteInPanel();
            }
        }

        void CheckIconCompleteInPanel()
        {
            bool inViewPanel=false;

            var day = item.day;

            var layer = (int)(day / 5.01f);

            var min = minHeight + (100) * layer;
            var max = maxHeight + (100) * layer;

            inViewPanel = scrollView.transform.localPosition.y <= max && min < scrollView.transform.localPosition.y;

            if (inViewPanel )
            {
                ps.gameObject.SetActive(true);
            }
            else
            {
                ps.gameObject.SetActive(false);
            }
        }
        #endregion
    }
}